package com.iot.gateway.tsl.controller;

import com.iot.gateway.tsl.qo.EventQo;
import com.iot.gateway.tsl.service.TslEventService;
import com.iot.gateway.utils.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物模型管理 - 事件管理
 * 提供设备事件的动态增删改查 REST 接口
 *
 * @author guoxinyu
 * @create 2025/5/27 18:40
 */
@RestController
@RequestMapping("/api/device/event")
@Tag(name = "物模型管理 - 事件管理")
public class TslEventController {

    private final TslEventService tslEventService;

    public TslEventController(TslEventService tslEventService) {
        this.tslEventService = tslEventService;
    }

    @Operation(summary = "新增设备事件", description = "为指定设备添加一个事件")
    @PostMapping("/add/{deviceId}")
    public ApiResult<Void> addEvent(
            @Parameter(description = "设备 ID", required = true, example = "device123")
            @PathVariable String deviceId,
            @Parameter(description = "事件信息", required = true)
            @RequestBody EventQo eventQo) {
        tslEventService.addEvent(deviceId, eventQo);
        return ApiResult.success();
    }

    @Operation(summary = "更新设备事件", description = "更新指定设备的某个事件")
    @PostMapping("/update/{deviceId}")
    public ApiResult<Void> updateEvent(
            @Parameter(description = "设备 ID", required = true, example = "device123")
            @PathVariable String deviceId,
            @Parameter(description = "事件信息，包含要更新的 ID", required = true)
            @RequestBody EventQo eventQo) {
        tslEventService.updateEvent(deviceId, eventQo);
        return ApiResult.success();
    }

    @Operation(summary = "删除设备事件", description = "删除指定设备的某个事件")
    @GetMapping("/del/{deviceId}/{eventId}")
    public ApiResult<Void> deleteEvent(
            @Parameter(description = "设备 ID", required = true, example = "device123")
            @PathVariable String deviceId,
            @Parameter(description = "事件 ID", required = true, example = "f47ac10b-58cc-4372-a567-0e02b2c3d479")
            @PathVariable String eventId) {
        tslEventService.deleteEvent(deviceId, eventId);
        return ApiResult.success();
    }

    @Operation(summary = "查询设备事件列表", description = "获取指定设备的所有事件")
    @GetMapping("/select/{deviceId}")
    public ApiResult<List<EventQo>> getEvents(
            @Parameter(description = "设备 ID", required = true, example = "device123")
            @PathVariable String deviceId) {
        List<EventQo> list = tslEventService.getEvents(deviceId);
        return ApiResult.success(list);
    }
}
